package aiingames;

import java.util.Map;

import org.apache.log4j.Logger;
import org.geotools.map.event.MapLayerListener;

import aiingames.simulation.agent.Agent;
import aiingames.simulation.agent.Ball;
import aiingames.simulation.agent.DummyRLAgent;
import aiingames.simulation.agent.RLAgent;
import aiingames.simulation.agent.State;
import aiingames.simulation.gui.Gui;
import aiingames.simulation.io.StateMapIO;
import aiingames.simulation.server.Server;

import com.vividsolutions.jts.geom.Coordinate;


public class Controller {

	private static final Logger log = Logger.getLogger(Controller.class);


	public static void main (String [] args) {

		Config.generateLookupTable();

		String shape = "environment.shp";
		String envDist ="environmentDistances.xml.gz";
		log.info("reading network.");
		
		log.info("done.");

		log.info("initializing gui.");
		Gui gui = new Gui();
		gui.setEnvironment(shape);
		log.info("done.");


		log.info("initializing server.");
		Server server = new Server(envDist,24*3600/Config.TIME_STEP_SIZE);
		log.info("done");
		
		

		log.info("reading old state map");
		// Map<String, State> map = null;
		Map<String, State> map = StateMapIO.readStateMap("stateMap.dat");
		log.info("done");

		Coordinate ac = new Coordinate(4,7);
		Coordinate bc = new Coordinate(4,5);

		Ball b = new Ball(1);
		Agent agent = new DummyRLAgent(0,b);
		((RLAgent)agent).setStateMap(map);
		server.addAgent(agent,ac);
		server.addAgent(b, bc);

		System.out.print("learning");
		//100000000
		for (double i = 0; i <100000000; i++) {
			server.run();
			server.resetAgent(agent,new Coordinate(4,7));
			server.resetAgent(b, new Coordinate(4,5));
			((RLAgent)agent).informEpisodeEnd();
			// 50000
			if (i % 50 == 0) {
				System.out.print(".");
				server.addGui(gui);
			} else {
				server.addGui(null);
			}
			// 500000
			if (i % 500 == 0 && i > 0) {
				log.info("writing state map to file: states " + ((RLAgent)agent).getStateMap().size());
				
				StateMapIO.writeStateMap("stateMap.dat",((RLAgent)agent).getStateMap() );
				
				log.info("done");
			}
		}

		System.out.println("done");
		System.out.println(agent);
		gui.shutdown();
		
		StateMapIO.writeStateMap("stateMap.dat",((RLAgent)agent).getStateMap() );

		}
	}
